package zhihui.mobile.presenter.register;

import android.content.Context;
import android.text.TextUtils;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import zhihui.mobile.bean.phone.CodeParams;
import zhihui.mobile.bean.phone.PhoneCode;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.phone.PhoneManager;
import zhihui.mobile.manager.register.RegisterManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.uitils.CheckUtils;

/**
 * Created by 张公铭 on 2017/7/11 0011.
 */

public class PhoneCodePresenter implements IPhoneCodeContract.IPhoneCodePresenter
{
    private Logger logger = Logger.getLogger(PhoneCodePresenter.class);
    private Context mContext;
    private IPhoneCodeContract.IPhoneCodeView codeView;
    private PhoneCode curCode = null;


    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        codeView = (IPhoneCodeContract.IPhoneCodeView) view;
    }

    @Override
    public void onResume()
    {
        codeView.showMessage("");
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void getMsg()
    {
        codeView.showLoading();
        CodeParams codeParams = new CodeParams();
        codeParams.setPhone(RegisterManager.getInstance().getCurRegister().getPhone());
        codeParams.setType(2);
        PhoneManager.getIntance().sendMessage(codeParams, new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                codeView.hindLoading();
                try
                {
                    String state = result.getString("state");
                    String message = result.getString("msg");
                    if (state.equals(Config.SUCCESS))
                    {
                        codeView.startTimer();
                        Gson gson = new Gson();
                        PhoneCode temp = gson.fromJson(result.getString("data"), PhoneCode.class);
                        curCode = new PhoneCode();
                        curCode.setCode(temp.getCode());
                        curCode.setTime(temp.getTime());
                        codeView.showMessage("验证码已发送，请注意查收！");
                        RegisterManager.getInstance().getCurRegister().setCode(temp.getCode());
                    } else
                    {
                        codeView.showMessage(message);
                    }
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    logger.error("PhoneCodePresenter getMsg json error:" + e.getMessage());
                    codeView.showMessage("解析数据错误！");
                }
            }

            @Override
            public void failed(String message)
            {
                codeView.hindLoading();
                codeView.showMessage(message);
            }
        });
    }

    @Override
    public void goPasswordFragment(String code)
    {
        String msg = CheckUtils.checkPhoneCode(code, curCode);
        if (!TextUtils.isEmpty(msg))
        {
            codeView.showMessage(msg);
            return;
        }
        codeView.goPasswordFragment();
    }
}
